﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Input.Test;
using System.Windows.Threading;
using WPFExcelReport.Helpers;

namespace WPFExcelReport
{
    public partial class PDCBase : UserControl
    {
        public PDCBase()
        {

        }
        public bool IsStartFromMachine = false;
        public bool falg = false,infalg =false,retry = false;
        public bool IsStartFromSpinLot = false;
        #region-------------Properties-----------------------

        public string sql = "";
        public DataTable TResult = null;
        public ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        private string _ABFAGANG;
        public string ABFAGANG
        {
            get
            {
                return _ABFAGANG;
            }
            set
            {
                _ABFAGANG = value;
            }
        }
        string ROWID = "";
        bool IsLoaded = false;

        public DataSet SResult = null;
        public MainViewModel MainViewModel;


        public SETINTEXDB SetData
        {
            get
            {
                return MainViewModel.SetData;
            }
        }
        public SETINTEXDB SetEnv
        {
            get
            {
                return MainViewModel.SetEnv;
            }
        }

        public SETINTEXDB SetProd
        {
            get
            {
                return MainViewModel.SetProd;
            }
        }
        public CollectionViewSource vwTPDC
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTPDC");
            }
        }
        public DataRow dwTPDC
        {
            get
            {
                if (vwTPDC.View == null)
                    return null;
                if ((DataRowView)vwTPDC.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwTPDC.View.CurrentItem).Row;
            }
        }
        public CollectionViewSource vwPDC
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwPDC");
            }
        }
        public DataRow dwPDC
        {
            get
            {
                if (vwPDC.View == null)
                    return null;
                if ((DataRowView)vwPDC.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwPDC.View.CurrentItem).Row;
            }
        }
        public BindingListCollectionView cvNOrder
        {
            get
            {
                DataGrid dlg = this.FindName("dlgNOrders") as DataGrid;
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlg.ItemsSource);
            }
        }
        TextBlock msgtxt
        {
            get
            {
                return (TextBlock)this.FindName("txtMessage");
            }
        }
        //double NOrder = 0;
        public string ABFSTEP
        {
            get
            {
                tb = this.FindName("txtABFSTEP") as TextBox;
                return Helpers.Helper.Left(tb.Text.ToString().PadRight(20), 20);
            }
            set
            {
                tb = this.FindName("txtABFSTEP") as TextBox;
                tb.Text = value;
            }
        }
        public string SPINLOT
        {
            get
            {
                tb = this.FindName("txtSPINLOT") as TextBox;
                return Helpers.Helper.Left(tb.Text.ToString().PadRight(10), 10);
            }
            set
            {
                tb = this.FindName("txtSPINLOT") as TextBox;
                tb.Text = value;
            }
        }
        public string MACHINEID
        {
            get
            {
                tb = this.FindName("txtMachineID") as TextBox;
                MainViewModel.MACHINEID = Helpers.Helper.Left(tb.Text.ToString().PadRight(8), 8);
                return MainViewModel.MACHINEID;
            }
            set
            {
                tb = this.FindName("txtMachineID") as TextBox;
                tb.Text = value;
                MainViewModel.MACHINEID = value;
            }
        }


        public string KRS
        {
            get
            {
                return MainViewModel.KRS;
            }
            set
            {
                MainViewModel.KRS = value;
            }
        }
        public string LFD
        {
            get
            {
                return MainViewModel.LFD;
            }
            set
            {
                MainViewModel.LFD = value;
            }
        }
        public string POS
        {
            get
            {
                return MainViewModel.POS;
            }
            set
            {
                MainViewModel.POS = value;
            }
        }
        public string STEP
        {
            get
            {
                return MainViewModel.STEP;
            }
            set
            {
                MainViewModel.STEP = value;
            }
        }
        /*
        public SQLiteDatabase SQLiteDatabase
        {
            get
            {
                return MainViewModel.SQLiteDatabase;
            }
        }*/
        public DataTable TBResult;
        #endregion

        /// <summary>
        /// SetProd 当前订单
        /// SetData保存所有订单，但是用于展示今日订单
        /// SetEnv保存所有订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public virtual void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                MainViewModel = (MainViewModel)DataContext;
                IsLoaded = true;

                vwPDC.View.CurrentChanged += vwPDC_CurrentChanged;

                /*
                sql = "SELECT* FROM ICC_MAXORD";
                TBResult = SQLiteDatabase.GetDataTable(sql);
                if (SetEnv.Tables.Contains("ICC_MAXORD"))
                {
                    SetEnv.Tables["ICC_MAXORD"].Clear();
                }
                SetEnv.Merge(TBResult);
               
                foreach (DataRow row in SetEnv.Tables["ICC_MAXORD"].Rows)
                {
                    NOrder = Convert.ToInt64(row["MAXORD"].ToString());
                }*/
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }

        public virtual void vwPDC_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                if (dwPDC == null)
                    return;
                string filter = " ABAID = '"+dwPDC["ABAID"].ToString()+"'";
                DataRow[] rows = SetData.ICC_PDC.Select(filter);
                DataTable tb = rows.CopyToDataTable();
                tb.TableName = "ICC_PDC";
                SetProd.Clear();
                SetProd.Merge(tb);
                Button btnE = this.FindName("btnEDIT") as Button;
                btnE.IsEnabled = true;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }        
        public virtual void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }

        public virtual void Save()
        {
            try
            {
                infalg = false;
                if (dwTPDC == null)
                {
                    MessageBox.Show("没有记录需要保存，请检查");
                    return;
                }

                //SQLiteDatabase.InsertSQLiteTable(dwTPDC);
                INTEXDBC.InsertICC_PDC(dwTPDC, 1, app.company);

                try
                {
                    // sql = "SELECT* FROM ICC_PDC WHERE ABAID='" + dwTPDC["ABAID"].ToString() + "'";
                    SResult = INTEXDBC.QueryICC_PDC(dwTPDC, 1);
                    if (msgtxt != null)
                    {
                        foreach (DataRow row in SResult.Tables[0].Rows)
                        {
                            string text = msgtxt.Text;
                            if (text.Length > 500)
                                text = text.Substring(400);
                            msgtxt.Text = row["AMBDEMA"].ToString() + "生产" + row["QTY"].ToString() + "kg"
                                + Environment.NewLine + text;
                        }

                    }
                    INTEXBL.CreateKSLEIST(app.company, SResult);
                }
                catch (Exception ex)
                {
                    Tracer.Error(ex.StackTrace, ex);
                    MessageBox.Show(ex.StackTrace, ex.Message);
                    infalg = true;
                }

                //INTEXDBC.InsertICC_PDC(dwTPDC,1,app.company);

                //SetData.Merge(SetProd);
                
                /*
                string path = @"C:\IntexReport\Data\ITPDC";
                INTEXBL.AutoConvert(path, "ITXPDC");*/
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(ex.StackTrace, ex.Message);
                infalg = true;
            }
        }
        public virtual void ReTry_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                retry = false;
                if (dwTPDC == null)
                    return; 
                btn = this.FindName("btnOK") as Button;
                DataSet SR = new DataSet();
                //ABFSTEP = dwTPDC["ABFSTEP"].ToString();
                /*
                if (SQLiteDatabase == null)
                    throw new Exception("本地数据库未启动");*/
                //SQLiteDatabase.ExecuteNonQuery("DELETE FROM ICC_PDC WHERE TRIM(ABFSTEP)='" + ABFSTEP.Trim() + "'");
                //INTEXDBC.DeleteICC_PDC(dwTPDC, 3);
                //INTEXDBC.DeleteKSLEIST(dwTPDC, 1);
                //INTEXDBC.UpdateABFOLGE(dwTPDC, 6);
                int idx = dwPDC["ABAID"].ToString().IndexOf('.');
                string AID = dwPDC["ABAID"].ToString().Substring(idx + 1).Trim();
                DataRow dw = SetData.ICC_PDC.NewRow();
                dw["ABFMANDANT"] = dwPDC["ABFMANDANT"].ToString();
                dw["ABFAUFKRS"] = dwPDC["ABFAUFKRS"].ToString();
                dw["ABFAUFLFD"] = dwPDC["ABFAUFLFD"].ToString();
                dw["ABFAUFPOS"] = dwPDC["ABFAUFPOS"].ToString();
                dw["ABFAFOLGE"] = dwPDC["ABFAFOLGE"].ToString();
                dw["ABAID"] = AID.ToString();
                INTEXDBC.DeleteKSLEIST(dw,2);
                double QTY = Convert.ToDouble(dwPDC["QTY"].ToString());
                SR = INTEXDBC.QueryABFOLGE(dwPDC,3);
                DataRow dwAB = SR.Tables[0].Rows[0];
                double ABFSOLLMG = Convert.ToDouble(dwAB["ABFSOLLMG"].ToString());
                double ABFIMG = Convert.ToDouble(dwAB["ABFIMG"].ToString());
                double ABFQTY = ABFIMG-QTY*1000;
                if (ABFQTY<0) ABFQTY = 0;
                dw["QTY"] = ABFQTY.ToString();
                if (ABFQTY == 0)
                {
                    dw["NEWDATE"] = "0";
                    dw["NEWTIME"] = "0";
                }
                else
                {
                    dw["NEWDATE"] = dwAB["ABFISTARTDAT"].ToString();
                    dw["NEWTIME"] = dwAB["ABFISTARTZT"].ToString();
                }
                if (ABFSOLLMG < ABFQTY)
                {
                    dw["CHGDATE"] = "0";
                    dw["CHGTIME"] = "0";
                    dw["PROCESSED"] = " ";
                }
                else
                {
                    dw["CHGDATE"] = dwAB["ABFIENDDAT"].ToString();
                    dw["CHGTIME"] = dwAB["ABFIENDZT"].ToString();
                    dw["PROCESSED"] = dwAB["ABFSTATUS"].ToString();
                }
                INTEXDBC.UpdateABFOLGE(dw, 7);
                foreach (DataRow row in SetProd.ICC_PDC.Rows)
                {
                    row["PROCESSED"] = " ";
                    INTEXDBC.UpdateICC_PDC(row, 1, app.company);
                    //SQLiteDatabase.InsertSQLiteTable(row);
                }
                INTEXBL.CreateKSLEIST(app.company, SetProd);
                /*
                //INTEXDBC.DeleteICC_PDC(dwTPDC, 3);
               
                string sql = "UPDATE ICC_PDC SET PROCESSED=' ' WHERE TRIM(ABFSTEP)='" + ABFSTEP.Trim() + "'";

                SQLiteDatabase.ExecuteNonQuery(sql);*/
                /*
                string path = @"C:\IntexReport\Data\ITXPDC";
                INTEXBL.AutoConvert(path, "ITXPDC");*/
                Grid gd = this.FindName("gdTPDC") as Grid;
                gd.IsEnabled = false;
                btn.IsEnabled = true;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(ex.StackTrace + "出现错误" + ex.Message);
                retry = true;
            }
        }
        public DataRow dwLastPDC;
        public virtual void NewPDC(DataRow dwInput, string _SPINLOT, string _ABFSTEP)
        {

            string _date = "", _time = "", _stime = "";
            Helper.GetSTime(ref _date, ref _time, ref _stime);
            //string PDCDate = "20150817";
            //string PDCDate = Helper.GetDateAddDays(_date, -1);
            DataRow dwData = null;
            dwData = SetProd.ICC_PDC.NewRow();
            if (dwInput == null)
            {
                dwData["ABFSTEP"] = _ABFSTEP;
                dwData["JP10"] = _SPINLOT;
                dwData["TERMNO"] = app.TERMNO;
                dwData["NEWDATE"] = _date;
                dwData["NEWTIME"] = _stime;
                dwData["NEWUSER"] = app.User;
                dwData["PDCDATE"] = "";
                //dwData["PDCDATE"] = dwData["NEWDATE"];
                dwData["PDCTIMES"] = dwData["NEWTIME"];
                dwData["PDCTIMET"] = dwData["NEWTIME"];
                dwData["PDCSTAFF"] = dwData["NEWUSER"];

                dwData["PDCSHIFT"] = "";
                dwData["CHGDATE"] = "0";
                dwData["CHGTIME"] = "0";
                dwData["CHGUSER"] = "0";
                dwData["ABFMANDANT"] = app.Mandant;
                dwData["ABKAUFART"] = "".PadRight(2);
                dwData["ABPWAKZ"] = "".PadRight(1);

                dwData["ABPART"] = "".PadRight(20);
                dwData["ABPVAR"] = "".PadRight(3);
                dwData["ABPFARBE"] = "".PadRight(10);

                dwData["ABPDESS"] = "".PadRight(10);
                dwData["ABPCOLORIT"] = "".PadRight(10);
                dwData["ABPBESCHICH"] = "".PadRight(10);

                dwData["ABFAUFKRS"] = "".PadRight(2);
                dwData["ABFAUFLFD"] = "".PadRight(6);
                dwData["ABFAUFPOS"] = "".PadRight(3);
                dwData["ABPWAKZ"] = "".PadRight(1);
                dwData["ABFAFOLGE"] = "".PadRight(3);
                dwData["ABFFIX"] = "".PadRight(1);
                dwData["ABFME"] = "".PadRight(1);
                dwData["ABFKSTKRS"] = "".PadRight(2);
                dwData["ABFEVARUNR"] = "".PadRight(2);
                dwData["ABFEVARHNR"] = "".PadRight(2);

                dwData["ABFMANUELKZ"] = "".PadRight(1);
                dwData["ABFAGANG"] = "".PadRight(15);
                dwData["ABFBZAGANG"] = "".PadRight(60);
                dwData["MKFTP"] = "".PadRight(4);
                dwData["QTY"] = "0";
                dwData["ABFSTART"] = " ";
                dwData["ABFEND"] = " ";
                dwData["MACHINEID"] = "".PadRight(8);
                dwData["ABFIMNRKRS"] = "".PadRight(2);
                dwData["ABFIMLFDNR"] = "".PadRight(4);
                dwData["ABFIMPOS"] = "".PadRight(2);
                dwData["PDCFLAG"] = "".PadRight(1);

            }
            else
            {
                dwData.ItemArray = dwInput.ItemArray;//增加一个新的作业采集行
                dwData["TERMNO"] = app.TERMNO;
                dwData["NEWDATE"] = _date;
                dwData["NEWTIME"] = _stime;
                dwData["NEWUSER"] = app.User;
                dwData["PDCTIMES"] = dwData["NEWTIME"];
                dwData["PDCTIMET"] = dwData["NEWTIME"];
                dwData["QTY"] = "0";
                //dwData["PDCDATE"] = "";
            }
            while (true)
            {
                LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "ITXPDC", 1);
                dwData["ABAID"] = app.TERMNO.Trim() + "." + LFD;
                SResult = INTEXDBC.QueryICC_PDC(dwData, 12);
                if (SResult.Tables[0].Rows.Count == 0)
                    break;
            }
            ComboBox cb = this.FindName("cmbShift") as ComboBox;//找到下拉框
            if (cb != null)//如果找到
            {
                if (cb.SelectedIndex >= 0)//如果选择了某个班次
                {
                    string shift = (cb.SelectedItem as ComboBoxItem).Tag.ToString();//获取班次代号
                    dwData["PDCSHIFT"] = shift;//对于记录，赋值班次
                }
            }

            dwData["PDCFLAG"] = "N";
            dwData["PROCESSED"] = " ";
            dwData["MKFTP"] = "0000";
            dwData["PDCTIME"] = 0;

            dwData["JP01"] = 0;
            dwData["JP02"] = 0;
            dwData["JP03"] = "";
            dwData["JP04"] = 0;
            dwData["JP05"] = 0;
            dwData["JP06"] = 0;
            dwData["JP07"] = 0;
            dwData["JP08"] = 0;
            dwData["JP09"] = 0;
            dwData["JP12"] = 0;
            dwData["JP13"] = 0;
            dwData["JP14"] = 0;
            dwData["JP15"] = 0;
            dwData["JP16"] = 0;
            dwData["JP17"] = 0;
            dwData["JP18"] = 0;
            dwData["JP19"] = 0;
            dwData["JP20"] = "";
            dwData["JP21"] = 0;
            dwData["JP22"] = 0;
            dwData["JP23"] = 0;
            dwData["JP24"] = "";
            dwData["JP25"] = "";
            dwData["JP26"] = "";
            dwData["JP27"] = "";
            dwData["JP28"] = "";
            dwData["JP29"] = 0;
            dwData["JP30"] = 0;
            SetProd.ICC_PDC.Rows.InsertAt(dwData, 0);
            vwTPDC.View.MoveCurrentToFirst();
            // NOrder++;
            /*
            sql = "UPDATE ICC_MAXORD SET MAXORD= '" + NOrder + "'";
            SQLiteDatabase.ExecuteNonQuery(sql);
            if (vwTPDC.View != null)
            {
                vwTPDC.View.MoveCurrentToLast();
            }*/
        }

        TextBox tb = null;
        Button btn = null;
        Button bReTry = null;
        public virtual void Load_Click(object sender, RoutedEventArgs e)
        {/*

            try
            {
                tb = this.FindName("txtKRS") as TextBox;
                string _krs = tb.Text.ToString();
                tb = this.FindName("txtLFD") as TextBox;
                string _lfd = tb.Text.ToString();
                tb = this.FindName("txtDateS") as TextBox;
                string dates = tb.Text.ToString().Trim();
                tb = this.FindName("txtQM") as TextBox;
                string qm = tb.Text.ToString().Trim();

                tb = this.FindName("txtDateE") as TextBox;
                string datee = tb.Text.ToString().Trim();

                string _date = "", _stime = "", _time = "";
                Helper.GetSTime(ref _date, ref _time, ref _stime);
                if (datee.Trim() == "")
                    datee = _date;
                if (dates.Trim() == "")
                    dates = Helper.GetDateAddDays(_date, -6);

                ///sql = "SELECT* FROM ICC_PDC WHERE ABFAGANG='" + ABFAGANG + "' AND TRIM(AMBDEMA)='" + qm + "' AND  ABFAUFKRS LIKE '" + _krs.Trim() + "%' AND ABFAUFLFD LIKE '" + _lfd.Trim() + "%' AND NEWDATE>='" + dates + "' AND NEWDATE<='" + datee + "'";
                //TBResult = SQLiteDatabase.GetDataTable(sql);
                DataRow dwData = SetProd.ICC_PDC.NewRow();
                dwData["ABFAGANG"] = ABFAGANG.Trim();
                dwData["AMBDEMA"] = qm.Trim();
                dwData["ABFAUFKRS"] = _krs.Trim();
                dwData["ABFAUFLFD"] = _lfd.Trim();
                dwData["NEWDATE"] = dates.Trim();
                dwData["CHGDATE"] = datee.Trim();
                SResult = INTEXDBC.QueryICC_PDC(dwData, 9);
                SetData.Clear();
                SetData.Merge(SResult);
                SetData.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }*/
        }
        public virtual void btnConfirmLine_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                infalg = false;
                Save();
                vwTPDC.View.MoveCurrentTo(-1);
                /*ComboBox cb = this.FindName("cmbShift") as ComboBox;//找到下拉框
                if (cb != null)//如果找到
                {
                    cb.SelectedIndex = -1;
                }*/

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(ex.StackTrace, ex.Message);
                infalg = true;
            }
        }
        public void SendToUIThread(UIElement element, string text)
        {
            element.Dispatcher.BeginInvoke(
                new Action(() => { SendKeys.Send(element, text); }),
                DispatcherPriority.Input
            );
        }
        public virtual void txtSPINLOT_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                falg = false;
                if (e.Key != System.Windows.Input.Key.Enter)
                { falg = true; return; }
                if (SPINLOT.Trim() == "")
                { falg = true; return; }
                if (ABFAGANG.Trim() != "XS")
                {
                    MessageBox.Show("不是细纱界面，禁止汇报");
                    falg = true; return;
                }
                if (dwTPDC == null)
                {
                    NewPDC(dwLastPDC, SPINLOT, ABFSTEP);
                    //IsStartFromMachine = false;
                    //IsStartFromSpinLot = true;
                }

                DataRow dwData = SetData.ICC_QUERYTB.NewRow();
                DataRow dwABF = null, dwGSDAT = null, dwABP = null;
                dwTPDC["ABFSTEP"] = ABFSTEP.ToUpper();
                dwTPDC["JP10"] = SPINLOT.ToUpper();

                KRS = ABFSTEP.Substring(0, 2);
                LFD = ABFSTEP.Substring(2, 6);
                POS = ABFSTEP.Substring(8, 3);
                STEP = ABFSTEP.Substring(11, 3);
                dwTPDC["ABFMANDANT"] = app.Mandant;
                dwTPDC["ABFAUFKRS"] = KRS;
                dwTPDC["ABFAUFLFD"] = LFD;
                dwTPDC["ABFAUFPOS"] = POS;
                dwTPDC["ABFAFOLGE"] = STEP;
                dwTPDC["ABFAGANG"] = ABFAGANG;
                dwTPDC["ABFBZAGANG"] = "";

                #region====ABFOLGE=======
                if (MainViewModel.OnLine)
                {
                    if (SPINLOT.Trim().Length > 1)
                    {
                        SResult = INTEXDBC.QueryABFOLGE(dwTPDC, 7);
                    }
                    else
                    {
                        SResult = INTEXDBC.QueryABFOLGE(dwTPDC, 6);
                    }
                    foreach (DataRow row in SResult.Tables[0].Rows)
                    {
                        dwABF = row;
                        break;
                    }
                    if (dwABF != null)
                    {
                        ABFSTEP = dwABF["ABFAUFKRS"].ToString() + dwABF["ABFAUFLFD"].ToString() + dwABF["ABFAUFPOS"].ToString() + dwABF["ABFAFOLGE"].ToString();
                        dwTPDC["ABFSTEP"] = ABFSTEP;
                        KRS = ABFSTEP.Substring(0, 2);
                        LFD = ABFSTEP.Substring(2, 6);
                        POS = ABFSTEP.Substring(8, 3);
                        STEP = ABFSTEP.Substring(11, 3);
                        dwTPDC["ABFAUFKRS"] = KRS;
                        dwTPDC["ABFAUFLFD"] = LFD;
                        dwTPDC["ABFAUFPOS"] = POS;
                        dwTPDC["ABFAFOLGE"] = STEP;
                        dwTPDC["ABFAGANG"] = dwABF["ABFAGANG"];
                        dwTPDC["ABFBZAGANG"] = dwABF["ABFBZAGANG"];

                        dwTPDC["ABFFIX"] = dwABF["ABFFIX"];
                        dwTPDC["ABFME"] = dwABF["ABFME"];

                        dwTPDC["JP07"] = dwABF["ABFIMG"];//找到未使用的JPXX
                        dwTPDC["JP08"] = dwABF["ABFDIFF"];
                        dwTPDC["JP09"] = dwABF["ABFSOLLMG"];
                        dwTPDC["ABFKSTKRS"] = dwABF["ABFKSTKRS"];
                        dwTPDC["ABFKST"] = dwABF["ABFKST"];

                        dwTPDC["ABFEVARUNR"] = dwABF["ABFEVARUNR"];
                        dwTPDC["ABFEVARHNR"] = dwABF["ABFEVARHNR"];
                        dwTPDC["ABFMANUELKZ"] = dwABF["ABFMANUELKZ"];

                        dwTPDC["ABKAUFART"] = dwABF["ABKAUFART"];
                        dwTPDC["ABPWAKZ"] = dwABF["ABPWAKZ"];
                        dwTPDC["ABPART"] = dwABF["ABPART"];
                        dwTPDC["ABPVAR"] = dwABF["ABPVAR"];
                        dwTPDC["ABPFARBE"] = dwABF["ABPFARBE"];
                        dwTPDC["ABPDESS"] = dwABF["ABPDESS"];
                        dwTPDC["ABPCOLORIT"] = dwABF["ABPCOLORIT"];
                        dwTPDC["ABPBESCHICH"] = dwABF["ABPBESCHICH"];
                        dwTPDC["JP01"] = dwABF["GSRECHNR"];
                        double JP01 = Convert.ToDouble(dwTPDC["JP01"].ToString());
                        if (JP01 == 0)
                        {
                            MessageBox.Show("纱支定义错误，请核对");
                            falg = true; return;
                        }
                        double N3 = Math.Round(100 * 590.5 / JP01, MidpointRounding.AwayFromZero) / 100;
                        dwTPDC["JP12"] = N3;
                        /*if (dwABF["MACHINEID"].ToString().Trim() != "000000" && !IsStartFromMachine)
                        {
                            dwTPDC["ABFIMNRKRS"] = dwABF["ABFIMNRKRS"];
                            dwTPDC["ABFIMLFDNR"] = dwABF["ABFIMLFDNR"];
                            dwTPDC["ABFIMPOS"] = dwABF["ABFIMPOS"];
                            dwTPDC["MACHINEID"] = dwABF["MACHINEID"];
                            MACHINEID = dwABF["MACHINEID"].ToString();
                            dwTPDC["AMAUFTRVON"] = dwABF["AMAUFTRVON"];
                            if (dwTPDC["AMAUFTRVON"].ToString().Trim() == "0")
                            {
                                dwTPDC["AMAUFTRVON"] = "";
                            }
                            dwTPDC["AMAUFTRBIS"] = dwABF["AMAUFTRBIS"];
                        }
                        else
                        {
                            if (app.User.Trim() == "ITXV")
                            {
                                //MACHINEID = "  022001";
                            }
                        }*/
                    }
                    else
                    {
                        //MainViewModel.OnLine = false;
                    }
                    if (dwTPDC["ABFBZAGANG"].ToString().Trim().Length == 0)
                    {
                        MessageBox.Show("没有找到对应生产单");
                        falg = true;
                        return;
                    }
                    else
                    {
                        falg = false;
                    }

                }
                #endregion

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }
        public virtual void txtABFSTEP_KeyUp(object sender, KeyEventArgs e)
        {
            txtSPINLOT_KeyUp(sender, e);
        }
        public virtual void LookForMachine(DataRow _TPDC)
        {
            SResult = INTEXDBC.QueryAMSTAMM(_TPDC, 3);
            foreach (DataRow dwAM in SResult.Tables[0].Rows)
            {
                _TPDC["MACHINEID"] = dwAM["MACHINEID"];
                _TPDC["AMAUFTRVON"] = dwAM["AMAUFTRVON"];
                _TPDC["AMAUFTRBIS"] = dwAM["AMAUFTRBIS"];
                _TPDC["MACHINEID"] = dwAM["MACHINEID"];
                _TPDC["ABFIMNRKRS"] = dwAM["AMNRKRS"];
                _TPDC["ABFIMLFDNR"] = dwAM["AMLFDNR"];
                _TPDC["ABFIMPOS"] = dwAM["AMPOS"];
                _TPDC["AMSTKBIS"] = dwAM["AMSTKBIS"];
                _TPDC["AMBDEMA"] = dwAM["AMBDEMA"];
                _TPDC["AMBEZ"] = dwAM["AMBEZ"];
                if (_TPDC["AMAUFTRVON"].ToString().Trim() == "0")
                {
                    _TPDC["AMAUFTRVON"] = "";
                }

                MainViewModel.AMBDEMA = dwAM["AMBDEMA"].ToString().Trim();
                MACHINEID = dwAM["MACHINEID"].ToString();
            }
        }
        public virtual void txtMACHINE_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key != System.Windows.Input.Key.Enter 
                    && e.Key != System.Windows.Input.Key.Enter)
                    return;
                TextBox tb = sender as TextBox;
                string mid = Helpers.Helper.Left(tb.Text.ToString().Trim().PadRight(8), 8);
                if (mid.Trim().Length == 0)
                    return;
                if (dwTPDC == null)
                {
                    NewPDC(dwLastPDC, SPINLOT, ABFSTEP);
                    //IsStartFromMachine = true;
                    //IsStartFromSpinLot = false;
                }

                dwTPDC["AMBDEMA"] = mid;
                dwTPDC["ABFAGANG"] = ABFAGANG;
                LookForMachine(dwTPDC);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public virtual void txtMTERS_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter)
                return;
            if (dwTPDC == null)
                return;

        }
        public virtual void txtSPINHS_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter)
                return;
            if (dwTPDC == null)
                return;
            try
            {

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #region


        public virtual void btnFilter_Click(object sender, RoutedEventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        public virtual void btnFilterClear_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                cvNOrder.CustomFilter = null;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }

        public virtual void Edit_Click(object sender, RoutedEventArgs e)
        {
            if (dwTPDC == null)
            {
                MessageBox.Show("请先选择需要修改的记录！");
                return;
            }
            TabItem tabE = this.FindName("tabEdit") as TabItem;
            tabE.Focus();
            tabE.IsEnabled = false;
            Button btnE = this.FindName("btnEDIT") as Button;
            btnE.IsEnabled = false;

            TabItem tab = this.FindName("tabPDC") as TabItem;
            tab.Focus();
            Grid gd = this.FindName("gdTPDC") as Grid;
            gd.IsEnabled = true;
            btn = this.FindName("btnOK") as Button;
            btn.IsEnabled = false;
            bReTry = this.FindName("ReTry") as Button;
            bReTry.IsEnabled = true;
        }

        public virtual void Del_Click(object sender, RoutedEventArgs e)
        {
            try
            {


            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }

        }
        #endregion


        public virtual void Start_Click(object sender, RoutedEventArgs e)
        {
            if (dwTPDC == null)
                return;
            dwTPDC["PDCFLAG"] = "S";
            if (dwTPDC["PDCTIMES"].ToString().Trim() == "0")
            {
                MessageBox.Show("必需输入开始时间");
            }
        }

        public virtual void End_Click(object sender, RoutedEventArgs e)
        {
            if (dwTPDC == null)
                return;
            dwTPDC["PDCFLAG"] = "T";
            if (dwTPDC["PDCTIMET"].ToString().Trim() == "0")
            {
                MessageBox.Show("必需输入结束时间");
            }
        }

        public virtual void btnResetScreen_Click(object sender, RoutedEventArgs e)
        {
            NewPDC(dwLastPDC, SPINLOT, ABFSTEP);
        }

        public virtual void PShift_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dwTPDC == null)
                return;
            ComboBox cb = sender as ComboBox;
            string shift = (cb.SelectedItem as ComboBoxItem).Tag.ToString();
            dwTPDC["PDCSHIFT"] = shift;
        }
    }
}
